/*
 * @(#)MtmFormPageFormSection.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.form.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.form.bean.MtmFormPageFormSection;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;

/**
  MtmFormPageFormSectionDAObject.java

  This class is the data access bean for table "MTM_FORM_PAGE_FORM_SECTION".

  @author      Scott Tong
  @company     DCIVision Limited
  @creation date   25/12/2003
  @version     $Revision: 1.11 $
*/

public class MtmFormPageFormSectionDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.11 $";

  public static final String TABLE_NAME = "MTM_FORM_PAGE_FORM_SECTION";

  public MtmFormPageFormSectionDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("SECTION_ID");
    this.vecDBColumn.add("PAGE_ID");
    this.vecDBColumn.add("FONT_TYPE");
    this.vecDBColumn.add("FONT_SIZE");
    this.vecDBColumn.add("FONT_STYLE");
    this.vecDBColumn.add("FONT_COLOR");
    this.vecDBColumn.add("POSITION_ROW");
    this.vecDBColumn.add("POSITION_COLUMN");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
    this.vecDBColumn.add("SECTION_SEQ");
  }

  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.SECTION_ID, A.PAGE_ID, A.FONT_TYPE, A.FONT_SIZE, A.FONT_STYLE, A.FONT_COLOR, A.POSITION_ROW, A.POSITION_COLUMN, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE,A.SECTION_SEQ ");
        sqlStat.append("FROM   MTM_FORM_PAGE_FORM_SECTION A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          MtmFormPageFormSection tmpMtmFormPageFormSection = new MtmFormPageFormSection();
          tmpMtmFormPageFormSection.setID(getResultSetInteger(rs, "ID"));
          tmpMtmFormPageFormSection.setSectionID(getResultSetInteger(rs, "SECTION_ID"));
          tmpMtmFormPageFormSection.setPageID(getResultSetInteger(rs, "PAGE_ID"));
          tmpMtmFormPageFormSection.setFontType(getResultSetString(rs, "FONT_TYPE"));
          tmpMtmFormPageFormSection.setFontSize(getResultSetInteger(rs, "FONT_SIZE"));
          tmpMtmFormPageFormSection.setFontStyle(getResultSetString(rs, "FONT_STYLE"));
          tmpMtmFormPageFormSection.setFontColor(getResultSetString(rs, "FONT_COLOR"));
          tmpMtmFormPageFormSection.setPositionRow(getResultSetInteger(rs, "POSITION_ROW"));
          tmpMtmFormPageFormSection.setPositionColumn(getResultSetInteger(rs, "POSITION_COLUMN"));
          tmpMtmFormPageFormSection.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmFormPageFormSection.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmFormPageFormSection.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmFormPageFormSection.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmFormPageFormSection.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmFormPageFormSection.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmFormPageFormSection.setSectionSeq(getResultSetInteger(rs, "SECTION_SEQ"));
          tmpMtmFormPageFormSection.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmFormPageFormSection.getCreatorID()));
          tmpMtmFormPageFormSection.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmFormPageFormSection.getUpdaterID()));
          return(tmpMtmFormPageFormSection);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.SECTION_ID, A.PAGE_ID, A.FONT_TYPE, A.FONT_SIZE, A.FONT_STYLE, A.FONT_COLOR, A.POSITION_ROW, A.POSITION_COLUMN, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE,A.SECTION_SEQ ");
        sqlStat.append("FROM   MTM_FORM_PAGE_FORM_SECTION A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          MtmFormPageFormSection tmpMtmFormPageFormSection = new MtmFormPageFormSection();
          tmpMtmFormPageFormSection.setID(getResultSetInteger(rs, "ID"));
          tmpMtmFormPageFormSection.setSectionID(getResultSetInteger(rs, "SECTION_ID"));
          tmpMtmFormPageFormSection.setPageID(getResultSetInteger(rs, "PAGE_ID"));
          tmpMtmFormPageFormSection.setFontType(getResultSetString(rs, "FONT_TYPE"));
          tmpMtmFormPageFormSection.setFontSize(getResultSetInteger(rs, "FONT_SIZE"));
          tmpMtmFormPageFormSection.setFontStyle(getResultSetString(rs, "FONT_STYLE"));
          tmpMtmFormPageFormSection.setFontColor(getResultSetString(rs, "FONT_COLOR"));
          tmpMtmFormPageFormSection.setPositionRow(getResultSetInteger(rs, "POSITION_ROW"));
          tmpMtmFormPageFormSection.setPositionColumn(getResultSetInteger(rs, "POSITION_COLUMN"));
          tmpMtmFormPageFormSection.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmFormPageFormSection.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmFormPageFormSection.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmFormPageFormSection.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmFormPageFormSection.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmFormPageFormSection.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmFormPageFormSection.setSectionSeq(getResultSetInteger(rs, "SECTION_SEQ"));
          tmpMtmFormPageFormSection.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmFormPageFormSection.getCreatorID()));
          tmpMtmFormPageFormSection.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmFormPageFormSection.getUpdaterID()));
          tmpMtmFormPageFormSection.setRecordCount(totalNumOfRecord);
          tmpMtmFormPageFormSection.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpMtmFormPageFormSection);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.SECTION_ID, A.PAGE_ID, A.FONT_TYPE, A.FONT_SIZE, A.FONT_STYLE, A.FONT_COLOR, A.POSITION_ROW, A.POSITION_COLUMN, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE,A.SECTION_SEQ ");
        sqlStat.append("FROM   MTM_FORM_PAGE_FORM_SECTION A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmFormPageFormSection tmpMtmFormPageFormSection = new MtmFormPageFormSection();
          tmpMtmFormPageFormSection.setID(getResultSetInteger(rs, "ID"));
          tmpMtmFormPageFormSection.setSectionID(getResultSetInteger(rs, "SECTION_ID"));
          tmpMtmFormPageFormSection.setPageID(getResultSetInteger(rs, "PAGE_ID"));
          tmpMtmFormPageFormSection.setFontType(getResultSetString(rs, "FONT_TYPE"));
          tmpMtmFormPageFormSection.setFontSize(getResultSetInteger(rs, "FONT_SIZE"));
          tmpMtmFormPageFormSection.setFontStyle(getResultSetString(rs, "FONT_STYLE"));
          tmpMtmFormPageFormSection.setFontColor(getResultSetString(rs, "FONT_COLOR"));
          tmpMtmFormPageFormSection.setPositionRow(getResultSetInteger(rs, "POSITION_ROW"));
          tmpMtmFormPageFormSection.setPositionColumn(getResultSetInteger(rs, "POSITION_COLUMN"));
          tmpMtmFormPageFormSection.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmFormPageFormSection.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmFormPageFormSection.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmFormPageFormSection.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmFormPageFormSection.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmFormPageFormSection.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmFormPageFormSection.setSectionSeq(getResultSetInteger(rs, "SECTION_SEQ"));
          tmpMtmFormPageFormSection.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmFormPageFormSection.getCreatorID()));
          tmpMtmFormPageFormSection.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmFormPageFormSection.getUpdaterID()));
          result.add(tmpMtmFormPageFormSection);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmFormPageFormSection tmpMtmFormPageFormSection = (MtmFormPageFormSection)((MtmFormPageFormSection)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   MTM_FORM_PAGE_FORM_SECTION(ID, SECTION_ID, PAGE_ID, FONT_TYPE, FONT_SIZE, FONT_STYLE, FONT_COLOR, POSITION_ROW, POSITION_COLUMN, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE, SECTION_SEQ) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpMtmFormPageFormSection.getSectionID());
        setPrepareStatement(preStat, 3, tmpMtmFormPageFormSection.getPageID());
        setPrepareStatement(preStat, 4, tmpMtmFormPageFormSection.getFontType());
        setPrepareStatement(preStat, 5, tmpMtmFormPageFormSection.getFontSize());
        setPrepareStatement(preStat, 6, tmpMtmFormPageFormSection.getFontStyle());
        setPrepareStatement(preStat, 7, tmpMtmFormPageFormSection.getFontColor());
        setPrepareStatement(preStat, 8, tmpMtmFormPageFormSection.getPositionRow());
        setPrepareStatement(preStat, 9, tmpMtmFormPageFormSection.getPositionColumn());
        setPrepareStatement(preStat, 10, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 11, new Integer(0));
        setPrepareStatement(preStat, 12, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 13, currTime);
        setPrepareStatement(preStat, 14, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 15, currTime);
        setPrepareStatement(preStat, 16, tmpMtmFormPageFormSection.getSectionSeq());
        preStat.executeUpdate();
        tmpMtmFormPageFormSection.setID(nextID);
        tmpMtmFormPageFormSection.setCreatorID(sessionContainer.getUserRecordID());
        tmpMtmFormPageFormSection.setCreateDate(currTime);
        tmpMtmFormPageFormSection.setUpdaterID(sessionContainer.getUserRecordID());
        tmpMtmFormPageFormSection.setUpdateDate(currTime);
        tmpMtmFormPageFormSection.setUpdateCount(new Integer(0));
        tmpMtmFormPageFormSection.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmFormPageFormSection.getCreatorID()));
        tmpMtmFormPageFormSection.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmFormPageFormSection.getUpdaterID()));
        return(tmpMtmFormPageFormSection);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmFormPageFormSection tmpMtmFormPageFormSection = (MtmFormPageFormSection)((MtmFormPageFormSection)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE MTM_FORM_PAGE_FORM_SECTION ");
        sqlStat.append("SET  SECTION_ID=?, PAGE_ID=?, FONT_TYPE=?, FONT_SIZE=?, FONT_STYLE=?, FONT_COLOR=?, POSITION_ROW=?, POSITION_COLUMN=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=?, SECTION_SEQ=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpMtmFormPageFormSection.getSectionID());
        setPrepareStatement(preStat, 2, tmpMtmFormPageFormSection.getPageID());
        setPrepareStatement(preStat, 3, tmpMtmFormPageFormSection.getFontType());
        setPrepareStatement(preStat, 4, tmpMtmFormPageFormSection.getFontSize());
        setPrepareStatement(preStat, 5, tmpMtmFormPageFormSection.getFontStyle());
        setPrepareStatement(preStat, 6, tmpMtmFormPageFormSection.getFontColor());
        setPrepareStatement(preStat, 7, tmpMtmFormPageFormSection.getPositionRow());
        setPrepareStatement(preStat, 8, tmpMtmFormPageFormSection.getPositionColumn());
        setPrepareStatement(preStat, 9, new Integer(tmpMtmFormPageFormSection.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 10, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 11, currTime);
        setPrepareStatement(preStat, 12, tmpMtmFormPageFormSection.getSectionSeq());
        setPrepareStatement(preStat, 13, tmpMtmFormPageFormSection.getID());
        setPrepareStatement(preStat, 14, tmpMtmFormPageFormSection.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpMtmFormPageFormSection.setUpdaterID(sessionContainer.getUserRecordID());
          tmpMtmFormPageFormSection.setUpdateDate(currTime);
          tmpMtmFormPageFormSection.setUpdateCount(new Integer(tmpMtmFormPageFormSection.getUpdateCount().intValue() + 1));
          tmpMtmFormPageFormSection.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmFormPageFormSection.getCreatorID()));
          tmpMtmFormPageFormSection.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmFormPageFormSection.getUpdaterID()));
          return(tmpMtmFormPageFormSection);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmFormPageFormSection tmpMtmFormPageFormSection = (MtmFormPageFormSection)((MtmFormPageFormSection)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   MTM_FORM_PAGE_FORM_SECTION ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpMtmFormPageFormSection.getID());
        setPrepareStatement(preStat, 2, tmpMtmFormPageFormSection.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpMtmFormPageFormSection);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    MtmFormPageFormSection tmpMtmFormPageFormSection = (MtmFormPageFormSection)this.oldValue;

    if (tmpMtmFormPageFormSection != null) {
      oldValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getID()));
      oldValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getSectionID()));
      oldValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getPageID()));
      oldValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getFontType()));
      oldValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getFontSize()));
      oldValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getFontStyle()));
      oldValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getFontColor()));
      oldValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getPositionRow()));
      oldValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getPositionColumn()));
      oldValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getUpdateDate()));
      oldValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getSectionSeq()));
    }

    tmpMtmFormPageFormSection = (MtmFormPageFormSection)obj;
    if (tmpMtmFormPageFormSection != null) {
      newValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getID()));
      newValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getSectionID()));
      newValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getPageID()));
      newValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getFontType()));
      newValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getFontSize()));
      newValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getFontStyle()));
      newValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getFontColor()));
      newValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getPositionRow()));
      newValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getPositionColumn()));
      newValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getUpdateDate()));
      newValues.add(toAuditTrailValue(tmpMtmFormPageFormSection.getSectionSeq()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/
  public synchronized AbstractBaseObject getObjectByPageIDSectionID(
      Integer formPageID,
      Integer formSectionID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.SECTION_ID, A.PAGE_ID, A.FONT_TYPE, A.FONT_SIZE, A.FONT_STYLE, A.FONT_COLOR, A.POSITION_ROW, A.POSITION_COLUMN, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE,A.SECTION_SEQ ");
        sqlStat.append("FROM   MTM_FORM_PAGE_FORM_SECTION A ");
        sqlStat.append("WHERE  A.PAGE_ID = ? AND A.SECTION_ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, formPageID);
        this.setPrepareStatement(preStat, 2, formSectionID);
        this.setPrepareStatement(preStat, 3, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          MtmFormPageFormSection tmpMtmFormPageFormSection = new MtmFormPageFormSection();
          tmpMtmFormPageFormSection.setID(getResultSetInteger(rs, "ID"));
          tmpMtmFormPageFormSection.setSectionID(getResultSetInteger(rs, "SECTION_ID"));
          tmpMtmFormPageFormSection.setPageID(getResultSetInteger(rs, "PAGE_ID"));
          tmpMtmFormPageFormSection.setFontType(getResultSetString(rs, "FONT_TYPE"));
          tmpMtmFormPageFormSection.setFontSize(getResultSetInteger(rs, "FONT_SIZE"));
          tmpMtmFormPageFormSection.setFontStyle(getResultSetString(rs, "FONT_STYLE"));
          tmpMtmFormPageFormSection.setFontColor(getResultSetString(rs, "FONT_COLOR"));
          tmpMtmFormPageFormSection.setPositionRow(getResultSetInteger(rs, "POSITION_ROW"));
          tmpMtmFormPageFormSection.setPositionColumn(getResultSetInteger(rs, "POSITION_COLUMN"));
          tmpMtmFormPageFormSection.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmFormPageFormSection.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmFormPageFormSection.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmFormPageFormSection.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmFormPageFormSection.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmFormPageFormSection.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmFormPageFormSection.setSectionSeq(getResultSetInteger(rs, "SECTION_SEQ"));
          tmpMtmFormPageFormSection.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmFormPageFormSection.getCreatorID()));
          tmpMtmFormPageFormSection.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmFormPageFormSection.getUpdaterID()));
          return(tmpMtmFormPageFormSection);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }

  }

  public synchronized AbstractBaseObject getObjectBySectionID(
      Integer formSectionID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.SECTION_ID, A.PAGE_ID, A.FONT_TYPE, A.FONT_SIZE, A.FONT_STYLE, A.FONT_COLOR, A.POSITION_ROW, A.POSITION_COLUMN, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE,A.SECTION_SEQ ");
        sqlStat.append("FROM   MTM_FORM_PAGE_FORM_SECTION A ");
        sqlStat.append("WHERE  A.SECTION_ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, formSectionID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          MtmFormPageFormSection tmpMtmFormPageFormSection = new MtmFormPageFormSection();
          tmpMtmFormPageFormSection.setID(getResultSetInteger(rs, "ID"));
          tmpMtmFormPageFormSection.setSectionID(getResultSetInteger(rs, "SECTION_ID"));
          tmpMtmFormPageFormSection.setPageID(getResultSetInteger(rs, "PAGE_ID"));
          tmpMtmFormPageFormSection.setFontType(getResultSetString(rs, "FONT_TYPE"));
          tmpMtmFormPageFormSection.setFontSize(getResultSetInteger(rs, "FONT_SIZE"));
          tmpMtmFormPageFormSection.setFontStyle(getResultSetString(rs, "FONT_STYLE"));
          tmpMtmFormPageFormSection.setFontColor(getResultSetString(rs, "FONT_COLOR"));
          tmpMtmFormPageFormSection.setPositionRow(getResultSetInteger(rs, "POSITION_ROW"));
          tmpMtmFormPageFormSection.setPositionColumn(getResultSetInteger(rs, "POSITION_COLUMN"));
          tmpMtmFormPageFormSection.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmFormPageFormSection.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmFormPageFormSection.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmFormPageFormSection.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmFormPageFormSection.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmFormPageFormSection.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmFormPageFormSection.setSectionSeq(getResultSetInteger(rs, "SECTION_SEQ"));
          tmpMtmFormPageFormSection.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmFormPageFormSection.getCreatorID()));
          tmpMtmFormPageFormSection.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmFormPageFormSection.getUpdaterID()));
          return(tmpMtmFormPageFormSection);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }

  }

  public synchronized List getListByPageID(
      Integer formPageID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.SECTION_ID, A.PAGE_ID, A.FONT_TYPE, A.FONT_SIZE, A.FONT_STYLE, A.FONT_COLOR, A.POSITION_ROW, A.POSITION_COLUMN, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE,A.SECTION_SEQ ");
        sqlStat.append("FROM   MTM_FORM_PAGE_FORM_SECTION A ");
        sqlStat.append("WHERE  A.PAGE_ID = ? AND A.RECORD_STATUS = ? ");
        sqlStat.append("ORDER BY A.SECTION_SEQ ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, formPageID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmFormPageFormSection tmpMtmFormPageFormSection = new MtmFormPageFormSection();
          tmpMtmFormPageFormSection.setID(getResultSetInteger(rs, "ID"));
          tmpMtmFormPageFormSection.setSectionID(getResultSetInteger(rs, "SECTION_ID"));
          tmpMtmFormPageFormSection.setPageID(getResultSetInteger(rs, "PAGE_ID"));
          tmpMtmFormPageFormSection.setFontType(getResultSetString(rs, "FONT_TYPE"));
          tmpMtmFormPageFormSection.setFontSize(getResultSetInteger(rs, "FONT_SIZE"));
          tmpMtmFormPageFormSection.setFontStyle(getResultSetString(rs, "FONT_STYLE"));
          tmpMtmFormPageFormSection.setFontColor(getResultSetString(rs, "FONT_COLOR"));
          tmpMtmFormPageFormSection.setPositionRow(getResultSetInteger(rs, "POSITION_ROW"));
          tmpMtmFormPageFormSection.setPositionColumn(getResultSetInteger(rs, "POSITION_COLUMN"));
          tmpMtmFormPageFormSection.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmFormPageFormSection.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmFormPageFormSection.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmFormPageFormSection.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmFormPageFormSection.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmFormPageFormSection.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmFormPageFormSection.setSectionSeq(getResultSetInteger(rs, "SECTION_SEQ"));
          tmpMtmFormPageFormSection.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmFormPageFormSection.getCreatorID()));
          tmpMtmFormPageFormSection.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmFormPageFormSection.getUpdaterID()));
          result.add(tmpMtmFormPageFormSection);
        }
      return(result);
    } catch (ApplicationException appEx) {
      throw appEx;
    } catch (SQLException sqle) {
      log.error(sqle, sqle);
      throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
    } catch (Exception e) {
      log.error(e, e);
      throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
    } finally {
      try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
      try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
    }
  }
}

}
